// do.go (已修正)
package main

import (
	"strconv" // <--- 导入 strconv

	pbc "github.com/Nik-U/pbc"
)

// DataOwner 代表数据所有者
type DataOwner struct {
	params *SystemParams
	PK     *PublicKey
	SK     *SecretKey
}

// NewDataOwner 创建一个新的数据所有者并生成密钥
func NewDataOwner(params *SystemParams) *DataOwner {
	// KeyGen
	x := params.Pairing.NewZr().Rand()
	v := params.Pairing.NewG1().NewFieldElement().PowZn(params.G, x)

	return &DataOwner{
		params: params,
		PK:     &PublicKey{V: v},
		SK:     &SecretKey{X: x},
	}
}

// TagGen 为文件生成标签
func (do *DataOwner) TagGen(fileData []byte, fileName string, blockSize int) ([]*pbc.Element, []*pbc.Element, *FileMetadata, error) {
	blocks := splitFileIntoBlocks(fileData, blockSize, do.params.Pairing)
	numBlocks := len(blocks)
	tags := make([]*pbc.Element, numBlocks)

	for i := 0; i < numBlocks; i++ {
		wiData := []byte(fileName + strconv.Itoa(i))
		h_wi := do.params.H(wiData)

		u_mi := do.params.Pairing.NewG1().PowZn(do.params.U, blocks[i])
		base := do.params.Pairing.NewG1().Mul(h_wi, u_mi)
		tags[i] = do.params.Pairing.NewG1().PowZn(base, do.SK.X)
	}

	metadata := &FileMetadata{
		Name:      fileName,
		NumBlocks: numBlocks,
	}

	return blocks, tags, metadata, nil
}
